package br.com.dubs.core.ejb;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import br.com.dubs.core.interfaces.ClientHandlerInterface;
import br.com.dubs.persistence.dao.ClientDAO;
import br.com.dubs.persistence.entities.ClientEntity;
import br.com.dubs.persistence.entities.Contact;
import br.com.dubs.persistence.exceptions.GenericDAOException;

@Stateless
public class ClientHandler implements ClientHandlerInterface {
	@EJB
	private ClientDAO clientDAO;

	public ClientEntity login(String user, String Password) {

		return clientDAO.checkLogin(user, Password);
	}

	public Boolean addClient(ClientEntity client) {
		try {
			clientDAO.add(client);
		} catch (GenericDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	public ClientEntity findClientByID(Integer Id){
		return clientDAO.load(new ClientEntity(), Id);
	}

	public Boolean addToContacts(Contact contact, ClientEntity client) {
		client.getContactList().getContactList().add(contact);
		try {
			clientDAO.update(client);
			return true;
		} catch (GenericDAOException e) {
			e.printStackTrace();
			return false;
		}
	}

	public List<ClientEntity> getClientList() {
		try {
			return clientDAO.listAll(ClientEntity.class, 0, 0);
		} catch (GenericDAOException e) {

			e.printStackTrace();
			return null;
		}
	}
}
